package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.enums.BorrowInfoStatusEnum;
import com.java.enums.BorrowerStatusEnum;
import com.java.enums.UserBindEnum;
import com.java.exceptioin.Assert;
import com.java.mapper.IntegralGradeMapper;
import com.java.mapper.UserInfoMapper;
import com.java.pojo.entity.BorrowInfo;
import com.java.mapper.BorrowInfoMapper;
import com.java.pojo.entity.IntegralGrade;
import com.java.pojo.entity.UserInfo;
import com.java.pojo.vo.BorrowInfoApprovalVo;
import com.java.pojo.vo.BorrowerInfoVo;
import com.java.result.ResponseEnum;
import com.java.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {

    @Resource
    private BorrowInfoMapper borrowInfoMapper;

    @Resource
    private IntegralGradeMapper integralGradeMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private DictService dictService;

    @Resource
    private LendService lendService;


    @Override
    public BigDecimal getAmount(Long userId) {

        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer integral = userInfo.getIntegral();

        QueryWrapper<IntegralGrade> wrapper = new QueryWrapper<>();
        wrapper.le("integral_start",integral)
                .ge("integral_end",integral);
        IntegralGrade integralGrade = integralGradeMapper.selectOne(wrapper);
        if(integralGrade == null){
            return new BigDecimal("0");
        }
        return integralGrade.getBorrowAmount();
    }

    @Override
    public void saveBorrowerInfo(BorrowInfo borrowInfo, Long userId) {

        UserInfo userInfo = userInfoMapper.selectById(userId);
        //校验绑定状态
        Assert.isTrue(userInfo.getBindStatus() == UserBindEnum.BIND_OK.getStatus(), ResponseEnum.USER_NO_BIND_ERROR);
        //校验认证状态
        Assert.isTrue(userInfo.getBorrowAuthStatus() == BorrowerStatusEnum.AUTH_OK.getStatus(),ResponseEnum.USER_NO_AMOUNT_ERROR);

        //校验用户输入的借款金额是否大于他的额度
        //查询额度
        BigDecimal amount = this.getAmount(userId);
        //如果借款 小于 他的额度 返回true 否则报错
        Assert.isTrue(amount.intValue() <= borrowInfo.getAmount().intValue(),ResponseEnum.USER_AMOUNT_LESS_ERROR);


        borrowInfo.setUserId(userId);
        //年利率需要转换成 0.12
        BigDecimal divide = borrowInfo.getBorrowYearRate().divide(new BigDecimal(100));//12 / 100
        borrowInfo.setBorrowYearRate(divide);
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());
        //保存borrowerInfo
        borrowInfoMapper.insert(borrowInfo);
    }

    @Override
    public Integer getBorrowerAuthStatus(Long userId) {
        QueryWrapper<BorrowInfo> wrapper = new QueryWrapper<>();
        wrapper.select("status").eq("user_id", userId);
        List<Object> objects = baseMapper.selectObjs(wrapper);
        if(objects.size() == 0){
            //借款人尚未提交信息
            return BorrowInfoStatusEnum.NO_AUTH.getStatus();
        }
        Integer status = (Integer)objects.get(0);
        return status;
    }

    @Override
    public IPage<BorrowerInfoVo> getBorrowInfoList(Page<BorrowerInfoVo> pageParam) {
        IPage page =  borrowInfoMapper.selectBorrowerList(pageParam);

        List<BorrowerInfoVo> list = page.getRecords();
        list.stream().forEach(item->{
            String returnMethod = dictService.getNameByParentDictCodeAndValue("returnMethod", item.getReturnMethod());
            item.setReturnMethodStr(returnMethod);
            String moneyUse = dictService.getNameByParentDictCodeAndValue("moneyUse", item.getMoneyUse());
            item.setMoneyUseStr(moneyUse);
        });
        return page;
    }

    @Override
    public void approval(BorrowInfoApprovalVo borrowInfoApprovalVo) {
        //设置审批状态
        Long id = borrowInfoApprovalVo.getId();
        BorrowInfo borrowInfo = borrowInfoMapper.selectById(id);
        borrowInfo.setStatus(borrowInfoApprovalVo.getStatus());
        borrowInfoMapper.updateById(borrowInfo);
        //新增投资标的
        if(borrowInfoApprovalVo.getStatus() == BorrowInfoStatusEnum.CHECK_OK.getStatus()){
            //TODO 标的需要管理 borrowInfoApprovalVo
            lendService.createLend(borrowInfoApprovalVo,borrowInfo);

        }
    }
}
